<template>
  <div class="user-profile">
     <van-nav-bar
      class="app-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="onBack"
    />
    <input type="file" ref="file" accept="image/*" hidden @change="onFileChange">
    <van-cell title="头像" is-link center @click="$refs.file.click()">
      <van-image width="30" height="30" round fit="cover" :src="user.photo"></van-image>
    </van-cell>
    <van-cell title="昵称" is-link :value="user.name" center @click="isEditNameShow = true"></van-cell>
    <van-cell title="性别" is-link :value="user.gender === 0 ? '男' : '女'" center @click="isEditGenderShow = true"></van-cell>
    <van-cell title="生日" is-link :value="user.birthday" center @click="isEditBirthdayShow = true"></van-cell>
    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name v-if="isEditNameShow" v-model="user.name"  @close="isEditNameShow = false"></update-name>
    </van-popup>
    <van-popup
      v-model="isEditGenderShow"
      position="bottom"
    >
      <update-gender v-if="isEditGenderShow" v-model="user.gender"  @close="isEditGenderShow = false"></update-gender>
    </van-popup>
    <van-popup
      v-model="isEditBirthdayShow"
      position="bottom"
    >
      <update-birthday v-if="isEditBirthdayShow" v-model="user.birthday"  @close="isEditBirthdayShow = false"></update-birthday>
    </van-popup>
    <van-popup
      class="update-photo-popup"
      v-model="isEditPhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo v-if="isEditPhotoShow" :file="previewImage" @update-photo="user.photo = $event" @close="isEditPhotoShow = false"></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user.js'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'UserProfileIndex',
  props: {},
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
    return {
      user: {},
      isEditNameShow: false,
      isEditGenderShow: false,
      isEditBirthdayShow: false,
      isEditPhotoShow: false,
      previewImage: null
    }
  },
  computed: {},
  watch: {},
  methods: {
    async loadUserProfie () {
      const { data } = await getUserProfile()
      this.user = data.data
    },
    onFileChange () {
      this.isEditPhotoShow = true
      const file = this.$refs.file.files[0]
      this.previewImage = file
      this.$refs.file.value = ''
    }
  },
  created () {
    this.loadUserProfie()
  },
  mounted () {
  },
  destroyed () {
  }
}
</script>

<style scoped lang='less'>
.van-popup {
  background-color: #f5f7f9;
}
.update-photo-popup {
  background-color: #000000;
}
</style>
